package com.pearadmin.pro.repository.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.base.page.Pageable;
import com.pearadmin.pro.repository.sys.SysHostRepository;
import com.pearadmin.pro.repository.sys.domain.*;
import com.pearadmin.pro.repository.sys.param.SysHostRequest;
import com.pearadmin.pro.repository.sys.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import static com.pearadmin.pro.common.tools.core.CmdUtil.execMacStatus;

/**
 * @author wangxiaozhe
 */
@Service
@SuppressWarnings("unused")
@Slf4j
public class SysHostServiceImpl extends ServiceImpl<SysHostRepository, SysHost> implements SysHostService {

    @Resource
    private SysHostRepository sysHostRepository;

    @Override
    public List<SysHost> list(SysHostRequest request) {
        return sysHostRepository.selectHost(request);
    }

    @Override
    public PageResponse<SysHost> page(SysHostRequest request) {
        List<SysHost> list = sysHostRepository.selectHost(request);
        list.forEach(
            host -> {
                // 查询具体机器信息，然后赋值
                SysInfo sysInfo = new SysInfo();
                String status = null;
                if (host.getHostName().equals("localhost")) {
                    log.info(host.getHostName() + "into brew services...");
                    try {
                        List lis = execMacStatus();
                        sysInfo.setInfo(lis.toString());
                    } catch (IOException | InterruptedException e) {
                        e.printStackTrace();
                    }
                    log.info(host.getHostName() + "out brew services...");
                }
                host.setSysInfo(sysInfo);
            }
        );
        log.info("end...");
        return Pageable.of(request,() -> list);
    }

    @Override
    public SysInfo sysInfo(SysHostRequest request) {
        SysInfo sysInfo = new SysInfo();
        sysInfo.setInfo("this is sys info~ ");
        return sysInfo;
    }
}
